<?php
/**
*@作者:MissZhang
*@邮箱:<787727147@qq.com>
*@创建时间:2021/7/3 上午12:43
*@说明:商品模型表
*/
namespace app\common\model;
use app\common\util\TpshopException;
use think\facade\Db;
use think\Model;

class GoodsType extends Model
{
    protected $pk = "id";
    // 关闭时间戳自动写入
    protected $autoWriteTimestamp = false;
    public function spec()
    {
        return $this->hasMany(Spec::class, 'type_id', 'id')->order('sort asc, id asc');
    }
    public function getPkAttr() {
        return $this->pk;
    }

    public function save(array $data = [], string $sequence = null): bool
    {
        $result = parent::save($data, $sequence);
        $spec = input('spec/a', []);
        foreach($spec as $spec_item){
            if(array_key_exists('id', $spec_item) && $spec_item['id'] > 0){
                $goodsSpec = Spec::find($spec_item['id']);
            }else{
                $goodsSpec = new Spec();
            }
            $goodsSpec->data($spec_item, true);
            $goodsSpec->type_id = $this->getAttr('id');
            $goodsSpec->save();
            $item_order_index = 0;
            if($spec_item['item']){
                foreach($spec_item['item'] as $item){
                    if(array_key_exists('id',$item) && $item['id'] > 0){
                        $specItem = SpecItem::find($item['id']);
                    }else{
                        $specItem = new SpecItem();
                    }
                    $specItem->data($item, true);
                    $specItem->order_index = $item_order_index;
                    $specItem->spec_id = $goodsSpec->id;
                    $specItem->save();
                    $item_order_index ++;
                }
            }
        }
        return $result;
    }
    public function delete(): bool
    {
        $id = $this->getAttr('id');
        $specs = Db::name('spec')->where('type_id', $id)->select();
        if($specs){
            $spec_item_ids = [];
            foreach ($specs as $spec) {
                $spec_items = Db::name('spec_item')->where('spec_id', $spec['id'])->select();
                if ($spec_items) {
                    foreach ($spec_items as $spec_item) {
                        array_push($spec_item_ids, $spec_item['id']);
                        $spec_goods_price = Db::name('spec_goods_price')->whereOr('key', $spec_item['id'])
                            ->whereOr('key', 'LIKE', '%\_' . $spec_item['id'])->whereOr('key', 'LIKE', $spec_item['id'] . '\_%')->find();
                        if ($spec_goods_price) {
                            $goods_name = Db::name('goods')->where('goods_id', $spec_goods_price['goods_id'])->value('goods_name');
                            throw new TpshopException('删除商品模型', 0, ['code' => 0, 'msg' => $goods_name . '在使用该规格项，不能删除']);
                        }
                    }
                }
            }
            Db::name('spec_item')->where('spec_id','IN',$spec_item_ids)->delete();
        }
        Db::name('spec')->where('type_id', $id)->delete();
        return parent::delete();
    }
}